$(document).ready(function() {
    var __ArtiForm = $('#form_article_add');
    var __editForm = $('#form_modal_edit');
    $('#submit_form').click(function() {

        var objArtiForm = {


            __formUrl: $("#push_address", __ArtiForm),
            __formName: $("input[name='push_module']", __ArtiForm)
                //__formTime : $("input[name='create_time']", __ArtiForm)
        }
        for (var i in objArtiForm) {

            if (objArtiForm[i].val() == '') {
                objArtiForm[i].css({
                    'border-color': 'red'
                });
                return false;
            } else {
                objArtiForm[i].css({
                    'border-color': '#e5e5e5'
                });
            }

        }

        $.ajax({
            type: "POST",
            url: "/Category/add",
            data: {
                "name": objArtiForm.__formName.val(),
                "sites_id": objArtiForm.__formUrl.val()
                    //"create_time" : objArtiForm.__formTime.val()
            },
            success: function(data) {
                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Category/list";
                });
            }
        });

    });

    $(document).on('click', '.tableDelete', function() {
        var userId = $(this).parent('td').attr('item_id'),
            thisUrl = $(this).attr('href');
        //官网欢迎页   
        layer.confirm('确认删除', {
            btn: ['确认', '取消'] //按钮
        }, function(index) {

            $.ajax({
                type: "GET",
                url: thisUrl,
                async: true,
                data: {
                    //"id" : userId
                },
                success: function(data) {
                    layer.alert(data.info, function() {
                        if (true === data.status) window.location = "/Category/list";
                    });
                }
            });
            layer.close(index);
            return false;

        }, function(index) {
            layer.close(index);
        });
        return false;

    });

    $(document).on('click', '.tableEdit', function(e) {

        $('#form_modal_edit').modal();
        var urlOld = $(this).attr('href'),
            urlArr = urlOld.split("/");
        urlId = urlArr[urlArr.length - 1];
        // $(this).attr({'href':'#form_modal_edit','data-toggle':'modal','data-target':'#form_modal_edit'});
        itemId = $(this).parent('td').attr('item_id');

        var __parent = $(this).parent('td').parent('tr.gradeX');
        objEditForm = {

            __formUrl: $("#pushAddress", __editForm),
            __formName: $('#pushModule')
                //__formTime : $('#create_time')
        };
        var tdTable = [];
        $('td', __parent).each(function() {
            tdTable.push($(this).text());
        });
        objEditForm.__formName.val(tdTable[1]);

        objEditForm.__formUrl.val(tdTable[0]);
        //objEditForm.__formTime.val(tdTable[2]);
        //

        $('option', objEditForm.__formUrl).each(function() {
            if ($(this).data('id') == itemId) {
                $(this).attr('selected', true);
            }
        });
        return false;

    });


    $('#edit_form').click(function() {
        var objAuthForm = {
            __formName: $("input[name='pushModule']", __editForm),
            __formUrl: $("#pushAddress", __editForm)
                //__formTime : $("#create_time", __editForm)
        }
        for (var i in objAuthForm) {

            if (objAuthForm[i].val() == '') {
                objAuthForm[i].css({
                    'border-color': 'red'
                });
                return false;
            } else {
                objAuthForm[i].css({
                    'border-color': '#e5e5e5'
                });
            }

        }
        $.ajax({
            type: "POST",
            url: "/Category/edit/id/" + urlId,
            data: {
                "name": objAuthForm.__formName.val(),
                "sites_id": 　objAuthForm.__formUrl.val()
                    // "create_time" :　objAuthForm.__formTime.val(),

            },
            success: function(data) {
                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Category/list";
                });
            },
            error: function(error) {
                alert('error');
            }
        });

    });

    //var nextBtn = $('#page_next');
    $(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
        var urlOld = $(this).attr('href'),
            urlArr = urlOld.split("/");
        urlId = urlArr[urlArr.length - 1];
        $.ajax({
            type: 'POST',
            url: '/Category/page/page/' + urlId,
            data: urlId,
            success: function(data) {
                var data = $.parseJSON(data);

                $('#tbody').html('');
                var id = '';

                $.each(data, function(k, v) {
                    //console.log('k='+k);
                    id = k;

                    $.each(v, function(kk, vv) {

                        $('#tbody').append($('<tr class="gradeX"><td>' + vv['url'] + '</td><td>' + vv['catname'] + '</td><td>' + vv['create_time'] + '</td><td item_id="' + vv['sites_id'] + '" cate_id="' + id + '"><a class="tableDelete" href="/Category/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Category/edit/id/' + id + '">修改</a> </td></tr>'));
                    });

                });

                //$('#tbody')
            }

        });
        $.ajax({
            type: 'POST',
            url: '/Category/currentPage/page/' + urlId,
            data: urlId,
            success: function(data) {
                //console.log(data);
                $('#pages').html(data);
                $('#pages').ready(function() {
                    nextBtn = $('#page_next');
                });
            }

        });
        return false;
    });

});